/*******************************************************************************
  ARTICLE	GAY, GOBBI, GONI (2025) "REVOLUTIONARY TRANSITIONS. INHERITANCE    
            CHANGE AND FERTILITY DECLINE" JOURNAL OF POLITICAL ECONOMY         
                                                                               
  AUTHORS	VICTOR GAY, PAULA GOBBI, MARC GONI                                 
  CONTACT	victor.gay@tse-fr.eu; paula.eugenia.gobbi@ulb.be; marc.goni@uib.no 
  VERSION	1.0 (MAY 2025)                                                     
  SOFTWARE	STATA SE 18                                                        
  LICENCE	MIT                                                                
--------------------------------------------------------------------------------

CADASTRE RESULTS DO FILE

This file contains the codes to reproduce the figures and tables in Appendix F
using the cadastre data.

Instructions: 
-------------
	open do-files from directory where they are placed; order matters; run whole code.

Contents: 
---------
	Table F2: Average land size.
	Figure F3: Distribution of parcels across size bins.
	Table F3: Inequality indices in the distribution of land.
	Figure F4: Cumulative distributions of parcel sizes.
	
Date last update: May 2025; Ran using STATA 18.5
*/
********************************************************************************

********************
* 0. PROGRAM SETUP *
********************

version 18
clear all
set more off

************************
* PACKAGE DEPENDENCIES *
************************

ssc install fre, replace
ssc install ineqdeco, replace

***************
* DIRECTORIES *
***************

global DAT 	= "../../3_outputs/3_1_datasets"
global APPTAB 	= "../../3_outputs/3_3_appendix/3_3_1_appendix_tables"
global APPFIG 	= "../../3_outputs/3_3_appendix/3_3_2_appendix_figures"
global TEMP = "../2_0_tempfiles"

timer on 1

* ==============================================================================
* Table F2: Average land size
* ------------------------------------------------------------------------------

* ABSOLUTE SIZE

** PLOT LEVEL

use "$DAT/cadastre.dta", clear
by treated, sort: summarize area_hec

** MUNICIPALITY LEVEL

use "$DAT/cadastre.dta", clear
collapse (mean) area_hec, by(villagenum treated)
by treated, sort: summarize area_hec

* RELATIVE SIZE

** PLOT LEVEL

use "$DAT/cadastre.dta", clear
by treated, sort: summarize area_sh

** MUNICIPALITY LEVEL

use "$DAT/cadastre.dta", clear
collapse (mean) area_sh, by(villagenum treated)
by treated, sort: summarize area_sh
* ==============================================================================

* ==============================================================================
* Figure F3: Distribution of parcels across size bins
* ------------------------------------------------------------------------------

* ABSOLUTE SIZE

use "$DAT/cadastre.dta", clear
fre plot_bin_hec if treated == 0
fre plot_bin_hec if treated == 1

/* manually input data */
import excel "../../1_raw_data/1_20_cadastre/cadastre_distribution.xlsx",    ///
	sheet("absolute") firstrow clear

graph bar sh_untreat sh_treat, over(size, label(angle(forty_five)))          ///
	   graphregion(fcolor(white) lcolor(white)) plotregion(fcolor(white)     ///
	   lcolor(black)) legend(label(1 "Egalitarian inheritance")              ///
	   label(2 "Inegalitarian inheritance")) ytitle("Share of plots (%)")    ///
	   legend(position(6) row(1))                                            ///
	   yscale(range(0 80)) ymtick(0(10)80) ylabel(0(10)80)                   ///
	   bar(1, color(navy)) bar(2, color(maroon))
graph export "$APPFIG/figureF3a.pdf", as(pdf) replace

* RELATIVE SIZE

use "$DAT/cadastre.dta", clear
fre plot_bin_sh if treated == 0
fre plot_bin_sh if treated == 1

/* manually input data */
import excel "../../1_raw_data/1_20_cadastre/cadastre_distribution.xlsx",    ///
	sheet("relative") firstrow clear
	
graph bar sh_untreat sh_treat, over(size, label(angle(forty_five)))          ///
	   graphregion(fcolor(white) lcolor(white)) plotregion(fcolor(white)     ///
	   lcolor(black)) legend(label(1 "Egalitarian inheritance")              ///
	   label(2 "Inegalitarian inheritance")) ytitle("Share of plots (%)")    ///
	   legend(position(6) row(1))                                            ///
	   yscale(range(0 80)) ymtick(0(10)80) ylabel(0(10)80)                   ///
	   bar(1, color(navy)) bar(2, color(maroon))
graph export "$APPFIG/figureF3b.pdf", as(pdf) replace
* ==============================================================================

* ==============================================================================
* Table F3: Inequality indices in the distribution of land
* ------------------------------------------------------------------------------

* INEQUALITY RATIOS
 
** LAND SIZE

use "$DAT/cadastre.dta", clear
ineqdeco area_hec if treated == 1
ineqdeco area_hec if treated == 0

** LAND VALUE

use "$DAT/cadastre.dta", clear
ineqdeco revenues if treated == 1
ineqdeco revenues if treated == 0

* GINI

** LAND SIZE

use "$DAT/cadastre.dta", clear
capture noisily ineqdeco area_hec, bygroup(treated)

** LAND VALUE

use "$DAT/cadastre.dta", clear
capture noisily ineqdeco revenues, bygroup(treated)
* ==============================================================================

* ==============================================================================
* Figure F4: Cumulative distributions of parcel sizes
* ------------------------------------------------------------------------------

use "$DAT/cadastre.dta", clear
replace area_hec = log(area_hec)

generate area_hec_untreated = .
replace area_hec_untreated = area_hec if treated == 0

generate area_hec_treated = .
replace area_hec_treated = area_hec if treated == 1

cumul area_hec_untreated, gen(c_untreated)
cumul area_hec_treated, gen(c_treated)

stack c_treated area_hec_treated c_untreated area_hec_untreated, into(c land) wide clear

line c_untreated c_treated land, sort  ///
	xtitle("Plot size (log hectares)", size(12-pt) height(6)) ytitle("")     ///
	yscale(range(0 1)) ymtick(0(0.2)1) xscale(range(-6 6))                   ///
	ylabel(0(.2)1, labsize(12-pt) format(%6.2fc))                            ///
	xmtick(-6(2)6) xlabel(-6(2)6, labsize(12-pt))           		         ///
	graphregion(fcolor(white) lcolor(white)) plotregion(fcolor(white)        ///
	lcolor(black)) legend(position(6) row(1) label(1 "Egalitarian inheritance") ///
	label(2 "Inegalitarian inheritance")) lwidth(thick thick) lcolor(navy maroon)
graph export "$APPFIG/figureF4.pdf", as(pdf) replace
* ==============================================================================

* KSIRNOV TEST

use "$DAT/cadastre.dta", clear
replace area_hec = log(area_hec)
ksmirnov area_hec, by(treated)

timer off 1 /* 3 seconds */
timer list